import heapq

from querier.esquerier import ElasticSearchQuerier
import json
import operator

WORDCLOUD_TOP_N = 100


class JDGoodsWordCloudQuerier(ElasticSearchQuerier):
    def __init__(self, es, index, doc_type):
        super(JDGoodsWordCloudQuerier, self).__init__(None, None, None)
        self.es = es
        self.index = index
        self.doc_type = doc_type

    def search(self, args):
        id_ = args['goods_id']
        res = self.es.get(index=self.index, doc_type=self.doc_type, id=id_)
        if res['found']:
            keywords = res['_source']['keywords'][0:WORDCLOUD_TOP_N]
            goods_id = res['_source']['goods_id']
            goods_name = res['_source']['goods_name']
            img_url = res['_source']['img_url']
            goods_url = res['_source']['goods_url']
            goods_start_date = res['_source']['goods_start_date']
            goods_end_date = res['_source']['goods_end_date']
            commission_ration_pc = res['_source']['commission_ratio_pc']
            commission_ration_wl = res['_source']['commission_ratio_wl']
            commission_pc = res['_source']['commission_pc']
            commission_wl = res['_source']['commission_wl']
            company_name = res['_source']['company_name']
            # company_promotion_url = res['_source']['company_promotion_url']
            thirty_order_count = res['_source']['thirty_order_count']
            intros = res['_source']['intros']
            comments = res['_source']['comments']
            price = res['_source']['price']
            shop_id = res['_source']['shop_id']
            thirty_commission_expense = res['_source']['thirty_commission_expense']
            unit_price_pc = res['_source']['unit_price_pc']
            unit_price_wl = res['_source']['unit_price_wl']
            created_at=res['_source']['created_at']
            updated_at=res['_source']['updated_at']

            return {'goods_id': goods_id,
                    'goods_name': goods_name,
                    'keywords': keywords,
                    'img_url': img_url,
                    'goods_url': goods_url,
                    'goods_start_date': goods_start_date,
                    'goods_end_date': goods_end_date,
                    'commission_ratio_pc': commission_ration_pc,
                    'commission_ratio_wl': commission_ration_wl,
                    'commission_pc': commission_pc,
                    'commission_wl': commission_wl,
                    'company_name': company_name,
                    # 'company_promotion_url': company_promotion_url,
                    'thirty_order_count': thirty_order_count,
                    'intros': intros,
                    'comments': comments,
                    'price': price,
                    'shop_id': shop_id,
                    'thirty_commission_expense': thirty_commission_expense,
                    'unit_price_pc': unit_price_pc,
                    'unit_price_wl': unit_price_wl,
                    'created_at':created_at,
                    'updated_at':updated_at

                    }
        return {'message': 'goods_id=%s not found.' % id_}
